﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

//System.WebHttpConte

using HGZP.PLL;
using HGZP.Model;

namespace HGZP.DAL
{
    public class CreateWorkOperatingLog
    {
        public CWorkOperatingLogCollection operatingLogRowCollection = new CWorkOperatingLogCollection();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ds_Operating">需要记录日志的DataSet</param>
        /// <param name="sOperatorObject">操作对象</param>
        /// <param name="sFalgColumnName">标志列</param>
        public void MyCreateWorkOperatingLog(DataSet ds_Operating, string sRemark)
        {
            //
            try
            {
                if (ds_Operating == null) { return; }
                //GetDataStructure myGetDataStructure = new GetDataStructure();
                #region 获取数据库结构
                DataSet ds_AllTableStructure = GetDataStructure.DataStructureAllTable;
                DataSet ds_AllColumnStructure = GetDataStructure.DataStructureAllColumn;
                #endregion
                //
                operatingLogRowCollection.F02_SelectHaveWhere(" 1 = 0 "); //取得空的数据集 准备新增数据
                // 
                #region 判断此表结构是否存在
                string sTableName = ds_Operating.Tables[0].TableName;//表的英文名称
                DataView dv_Column = new DataView(ds_AllColumnStructure.Tables[0]);
                dv_Column.RowFilter = " 表名 = '" + sTableName + "' ";
                if (dv_Column.Count == 0)
                {
                    throw new Exception("没有找到表" + sTableName + "的结构信息");
                }
                #endregion
                //
                #region 获取表的中文名称
                string sTableName_Cn = string.Empty; //表的中文名称
                DataView dv_Table = new DataView(ds_AllTableStructure.Tables[0]);
                dv_Table.RowFilter = " sTableID = '" + sTableName + "' ";
                if (dv_Table.Count != 0)
                {
                    sTableName_Cn = dv_Table[0]["sTableName"].ToString().Trim();
                    //如果表的描述很长 就去掉(后面的
                    if (sTableName_Cn.IndexOf("(") >= 0)
                    {
                        sTableName_Cn = sTableName_Cn.Substring(0, sTableName_Cn.IndexOf("("));
                    }
                }
                #endregion
                //
                MyOperatorType sOperatorType = MyOperatorType.新增;
                string sReportValue = string.Empty;
                string sTemp = string.Empty;
                string sOldValue = string.Empty;
                string sNewValue = string.Empty;
                string sColumnRemark = string.Empty;//列的说明
                //
                #region 循环各行数据  插入日志表
                foreach (DataRow r in ds_Operating.Tables[0].Rows)
                {
                    if (r.RowState == DataRowState.Added) { sOperatorType = MyOperatorType.新增; }
                    if (r.RowState == DataRowState.Deleted) { sOperatorType = MyOperatorType.删除; }
                    if (r.RowState == DataRowState.Modified) { sOperatorType = MyOperatorType.修改; }
                    sReportValue = string.Empty;
                    sOldValue = string.Empty;
                    sNewValue = string.Empty;
                    int iCount = 0;
                    //
                    //
                    #region 循环各个列 形成日志字符串
                    foreach (DataRowView drv in dv_Column)
                    {
                        //
                        #region 新增
                        if (sOperatorType == MyOperatorType.新增)
                        {
                            if (drv["标识"].ToString().Trim() == "√") { continue; }
                            sNewValue = r[drv["字段名"].ToString().Trim()].ToString().Trim();
                            if (drv["类型"].ToString().Trim() == "bit")
                            {
                                if (sNewValue == "False") { sNewValue = "否"; }
                                if (sNewValue == "True") { sNewValue = "是"; }
                            }
                            if (drv["字段名"].ToString().IndexOf(SpecialColumn.sGuid) >= 0) { continue; }
                            sColumnRemark = drv["字段说明"].ToString().Trim();
                            if (sColumnRemark.IndexOf("(") >= 0)
                            {
                                sColumnRemark = sColumnRemark.Substring(0, sColumnRemark.IndexOf("("));
                            }
                            sTemp = "(" + sColumnRemark + ":" + sNewValue + ")";
                            sReportValue = sReportValue + sTemp;
                            iCount++;


                        }
                        #endregion

                        #region 删除
                        if (sOperatorType == MyOperatorType.删除)
                        {

                            sOldValue = r[drv["字段名"].ToString().Trim(), DataRowVersion.Original].ToString().Trim();
                            if (drv["类型"].ToString().Trim() == "bit")
                            {
                                if (sOldValue == "False") { sOldValue = "否"; }
                                if (sOldValue == "True") { sOldValue = "是"; }
                            }
                            if (drv["字段名"].ToString().IndexOf(SpecialColumn.sGuid) >= 0) { continue; }
                            sColumnRemark = drv["字段说明"].ToString().Trim();
                            if (sColumnRemark.IndexOf("(") >= 0)
                            {
                                sColumnRemark = sColumnRemark.Substring(0, sColumnRemark.IndexOf("("));
                            }
                            sTemp = "(" + sColumnRemark + ":" + sOldValue + ")";
                            sReportValue = sReportValue + sTemp;
                            iCount++;

                        }
                        #endregion

                        #region 修改
                        if (sOperatorType == MyOperatorType.修改)
                        {
                            //
                            sOldValue = r[drv["字段名"].ToString().Trim(), DataRowVersion.Original].ToString().Trim();
                            sNewValue = r[drv["字段名"].ToString().Trim()].ToString().Trim();
                            if (drv["类型"].ToString().Trim() == "bit")
                            {
                                if (sOldValue == "False") { sOldValue = "否"; }
                                if (sOldValue == "True") { sOldValue = "是"; }
                                if (sNewValue == "False") { sNewValue = "否"; }
                                if (sNewValue == "True") { sNewValue = "是"; }
                            }
                            if (drv["字段名"].ToString().IndexOf(SpecialColumn.sGuid) >= 0) { continue; }
                            if (r[drv["字段名"].ToString().Trim(), DataRowVersion.Original].ToString().Trim() == r[drv["字段名"].ToString().Trim()].ToString().Trim()) { continue; }
                            sColumnRemark = drv["字段说明"].ToString().Trim();
                            if (sColumnRemark.IndexOf("(") >= 0)
                            {
                                sColumnRemark = sColumnRemark.Substring(0, sColumnRemark.IndexOf("("));
                            }
                            sTemp = "(" + sColumnRemark + ":" + sOldValue + "-> " + sNewValue + ")";
                            sReportValue = sReportValue + sTemp;
                            iCount++;

                        }
                        #endregion

                    }
                    #endregion

                    //
                    //
                    #region 获取标识列  sTitle
                    string sTitle = string.Empty; //标识列 sName 或者 sLogName
                    string sFilterString_Title = " 表名 = '{0}' and  字段名 = '{1}' ";
                    sFilterString_Title = string.Format(sFilterString_Title, sTableName, SpecialColumn.sName);
                    dv_Column.RowFilter = sFilterString_Title;
                    if (dv_Column.Count != 0)
                    {
                        if (r.RowState == DataRowState.Deleted)
                        {
                            sTitle = r[SpecialColumn.sName, DataRowVersion.Original].ToString();// dv_Column[0][SpecialColumn.sName].ToString();
                        }
                        else
                        {
                            sTitle = r[SpecialColumn.sName].ToString();
                        }
                    }
                    else
                    {
                        sFilterString_Title = string.Format(sFilterString_Title, sTableName, SpecialColumn.sLogName);
                        dv_Column.RowFilter = sFilterString_Title;
                        if (dv_Column.Count != 0)
                        {
                            if (r.RowState == DataRowState.Deleted)
                            {
                                sTitle = r[SpecialColumn.sLogName, DataRowVersion.Original].ToString();
                            }
                            else
                            {
                                sTitle = r[SpecialColumn.sLogName].ToString();
                            }
                        }

                    }
                    #endregion

                    //
                    #region 有变化的话 插入日志表
                    if (iCount != 0)
                    {
                        MyParameter selfInfo = (MyParameter)System.Web.HttpContext.Current.Session["OperatorInfo"];
                        //将MyParameter 类保存在Session 里面
                        CWorkOperatingLog myCWorkOperatingLog = new CWorkOperatingLog();
                        myCWorkOperatingLog.sGuid = MyFunction.GetNewGuid();
                        myCWorkOperatingLog.dCreateDate = DateTime.Now;//GetDateTime.GetServerDateTime(); 因为是Web程序 所以用本地时间
                        myCWorkOperatingLog.sTitle = sTitle;
                        myCWorkOperatingLog.sGuidtBaseOperator = "zhwl";//selfInfo.OperationGuid;
                        myCWorkOperatingLog.sNametBaseOperator = "郑文亮";//selfInfo.OperationName;
                        myCWorkOperatingLog.sIP = "ddd";//selfInfo.OperationIP;
                        myCWorkOperatingLog.sMAC = "ddd";//selfInfo.OperationMAC;
                        myCWorkOperatingLog.sOperatorObject = sTableName_Cn;
                        myCWorkOperatingLog.sOperatorType = sOperatorType.ToString();
                        myCWorkOperatingLog.sValue = sReportValue;
                        myCWorkOperatingLog.sRemark = sRemark;
                        operatingLogRowCollection.F30_Insert(myCWorkOperatingLog);
                    }
                    #endregion

                    //
                }
                #endregion


                //
            }
            catch (Exception e)
            {
                throw new Exception(e.Message.ToString());
            }

            //
        }
        public void MyCreateWorkLog(DataTable dt_Operating, string sRemark)
        {
            DataSet ds_Current = new DataSet();
            ds_Current.Tables.Add(dt_Operating.GetChanges());
            MyCreateWorkOperatingLog(ds_Current, sRemark);
        }
    }
    public enum MyOperatorType
    {
        新增,
        删除,
        修改
    }
    static public class SpecialColumn
    {
        //特殊列的名称
        static public string sGuid = "sGuid".Trim();
        static public string iSort = "iSort".Trim();
        static public string sName = "sName".Trim();
        static public string bValid = "bValid".Trim();
        static public string sLogName = "sLogName".Trim();
    }
}
